package com.ideaaedi.zoo.diy.artifact.flowable;

import com.ideaaedi.zoo.diy.artifact.flowable.config.ZooEngineConfigurationConfigurer;
import com.ideaaedi.zoo.diy.artifact.flowable.properties.ZooFlowableDIYGuideProperties;
import com.ideaaedi.zoo.diy.artifact.flowable.service.form.BpmnFormRepositoryService;
import com.ideaaedi.zoo.diy.artifact.flowable.service.form.BpmnFormService;
import com.ideaaedi.zoo.diy.artifact.flowable.service.impl.BpmnFormServiceImpl;
import org.flowable.engine.FormService;
import org.flowable.engine.HistoryService;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * flowable 自动配置
 */
@EnableConfigurationProperties(ZooFlowableDIYGuideProperties.class)
@ConditionalOnProperty(value = ZooFlowableAutoConfiguration.ENABLE, havingValue = "true", matchIfMissing = true)
public class ZooFlowableAutoConfiguration {

    public static final String ENABLE = "zoo.switch.artifact-flowable.enable";

    /**
     * 如果你项目中需要使用{@link BpmnFormService}，那么
     * 你需要实现{@link BpmnFormRepositoryService}来增删改查表单
     */
    @Bean
    @ConditionalOnMissingBean
    public BpmnFormService bpmnFormService(FormService formService, HistoryService historyService,
                                           ObjectProvider<BpmnFormRepositoryService> formRepositoryServiceProvider) {
        return new BpmnFormServiceImpl(formService, historyService, formRepositoryServiceProvider.getIfAvailable());
    }

    @Bean
    @ConditionalOnMissingBean
    public ZooEngineConfigurationConfigurer zooEngineConfigurationConfigurer() {
        return new ZooEngineConfigurationConfigurer();
    }

}